package com.ea.common.handler;

import com.ea.common.exception.ServiceException;
import com.ea.common.response.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author NAME-L
 * @Description 自定义异常拦截器
 * @className GlobalExceptionHandler
 * @date 2022-12-08 00:42:20
 */

@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {


//    @ExceptionHandler(value = ServiceException.class)
//    @ResponseBody
//    public JsonResult serviceExceptionHandler(ServiceException e){
//        log.error(e.getMessage(), e);
//        return JsonResult.errorData(e.getErrorCode(), e.getErrorMsg());
//    }

    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public ResponseEntity<JsonResult> handleOpdRuntimeException(ServiceException e) {
        return ResponseEntity.status(HttpStatus.valueOf(Integer.parseInt(e.getErrorCode()))).body(JsonResult.errorData( e.getErrorCode(), e.getErrorMsg()));
    }

}
